/*
 * Copyright (c) 2023. All rights reserved.
 * Please don't try to modify the code, because I don't know what the consequences will be.
 *
 *
 *
 *
 *
 *
 *
 */

package cc.zzone.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息转换器配置
 *
 * @author jinzhengze
 * @since 2023年02月01日
 */
@Configuration
public class MessageConverterConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(3, fastJsonHttpMessageConverter());
    }

    private FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setSupportedMediaTypes(listMediaType());
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteEnumUsingToString,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue
        );
        ValueFilter valueFilter = (object, name, value) -> null == value ? "" : value;
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializeFilters(valueFilter);
        converter.setFastJsonConfig(fastJsonConfig);
        return converter;
    }

    private List<MediaType> listMediaType() {
        List<MediaType> mt = new ArrayList<>();
        mt.add(MediaType.APPLICATION_JSON);
        mt.add(MediaType.APPLICATION_ATOM_XML);
        mt.add(MediaType.APPLICATION_FORM_URLENCODED);
        mt.add(MediaType.APPLICATION_OCTET_STREAM);
        mt.add(MediaType.APPLICATION_PDF);
        mt.add(MediaType.APPLICATION_RSS_XML);
        mt.add(MediaType.APPLICATION_XHTML_XML);
        mt.add(MediaType.APPLICATION_XML);
        mt.add(MediaType.IMAGE_GIF);
        mt.add(MediaType.IMAGE_JPEG);
        mt.add(MediaType.IMAGE_PNG);
        mt.add(MediaType.TEXT_EVENT_STREAM);
        mt.add(MediaType.TEXT_HTML);
        mt.add(MediaType.TEXT_MARKDOWN);
        mt.add(MediaType.TEXT_PLAIN);
        mt.add(MediaType.TEXT_XML);
        mt.add(MediaType.ALL);
        mt.add(new MediaType("application", "*+json"));
        return mt;
    }
}
